package bbs.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xkh.xkh.R;
import com.orm.SugarRecord;
import java.util.List;

import bbs.model.Subject;
import bbs.model.Submit;
import bbs.net.SendTiezi;
import managers.UIHandler;
import model.OwnUser;
import utils.TimeUtil;

/**
 * 发帖
 * Created by nimi on 2015/11/12.
 */
public class NewPostActivity extends UIHandler.BaseActivity implements View.OnClickListener, SendTiezi.Delegate {
    private Button back_subject;
    private TextView bbs_title;
    private ImageButton btn_new;
    private EditText ed_title;
    private EditText et_post;
    Long subjectId;
    private SendTiezi sendTiezi;
    private SendTiezi.Upload upload = new SendTiezi.Upload();

    OwnUser ownUser;

    {
        List<OwnUser> ownUsers = SugarRecord.listAll(OwnUser.class);
        if (!ownUsers.isEmpty()) {
            ownUser = ownUsers.get(0);
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bbs_new);
        Bundle bundle = getIntent().getExtras();
        subjectId = bundle.getLong("subjectId");
        String subjectName = bundle.getString("subjectName");
        bbs_title = (TextView) findViewById(R.id.bbs_title);
        bbs_title.setText(subjectName);
        back_subject = (Button) findViewById(R.id.back_bbs_subject);
        back_subject.setOnClickListener(this);
        btn_new = (ImageButton) findViewById(R.id.btn_new);
        btn_new.setOnClickListener(this);
        ed_title = (EditText) findViewById(R.id.et_title);
        et_post = (EditText) findViewById(R.id.et_post);

    }

    public void onClick(View v) {
        if (v.getId() == R.id.back_bbs_subject) {
//            Intent intent=new Intent(NewPostActivity.this,OneSubjectActivity.class);
//            startActivity(intent);
            finish();
        } else if (v.getId() == R.id.btn_new) {
            if (check()) {
                Submit submit = new Submit();
                submit.setBoard_Title(ed_title.getText().toString());
                submit.setBoard_Content(et_post.getText().toString());
                submit.setBoard_IssueTime(TimeUtil.currentTime().toString());
                Subject subject = new Subject();
                subject.setTopic_ID(subjectId);
                submit.setTopic_ID(subjectId + "");
                submit.setUser_ID(ownUser.getUser_ID());
                upload.setSubmit(submit);
                sendTiezi = new SendTiezi(upload);
                sendTiezi.setDelegate(NewPostActivity.this);
                sendTiezi.get();


            }
        }


    }

    public boolean check() {
        if (TextUtils.isEmpty(ed_title.getText().toString())) {
            Toast.makeText(this, "标题为空",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(et_post.getText().toString())) {
            Toast.makeText(this, "内容为空",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void onResponse(String response, SendTiezi sendTiezi) {
        if (sendTiezi.getDownload().getStatus() == 1)
            Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
//        Intent intent=new Intent(NewPostActivity.this,OneSubjectActivity.class);
//        startActivity(intent);
        finish();
    }

    public void onErrorResponse(VolleyError error, SendTiezi sendTiezi) {

    }

}
